<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="书名" prop="bookName">
        <el-input
          v-model="queryParams.bookName"
          placeholder="请输入书名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版时间" prop="bookTime">
        <el-input
          v-model="queryParams.bookTime"
          placeholder="请输入出版时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="bookAuthor">
        <el-input
          v-model="queryParams.bookAuthor"
          placeholder="请输入作者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="豆瓣评分" prop="bookRank">
        <el-input
          v-model="queryParams.bookRank"
          placeholder="请输入豆瓣评分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="isbn" prop="bookIsbn">
        <el-input
          v-model="queryParams.bookIsbn"
          placeholder="请输入isbn"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="封面" prop="bookImg">
        <el-input
          v-model="queryParams.bookImg"
          placeholder="请输入封面"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="展现时间" prop="upTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.upTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择展现时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="up数量" prop="upNum">
        <el-input
          v-model="queryParams.upNum"
          placeholder="请输入up数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="up数量" prop="upState">
        <el-input
          v-model="queryParams.upState"
          placeholder="请输入up数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gt:gtbook:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gt:gtbook:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gt:gtbook:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gt:gtbook:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="gtbookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="book_id" align="center" prop="bookId" />
      <el-table-column label="书名" align="center" prop="bookName" />
      <el-table-column label="出版时间" align="center" prop="bookTime" />
      <el-table-column label="作者" align="center" prop="bookAuthor" />
      <el-table-column label="豆瓣评分" align="center" prop="bookRank" />
      <el-table-column label="isbn" align="center" prop="bookIsbn" />
      <el-table-column label="封面" align="center" prop="bookImg" />
      <el-table-column label="多图" align="center" prop="bookImgs" />
      <el-table-column label="图书简介" align="center" prop="bookIntro" />
      <el-table-column label="作者简介" align="center" prop="bookAuthorIntro" />
      <el-table-column label="展现时间" align="center" prop="upTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.upTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="up数量" align="center" prop="upNum" />
      <el-table-column label="up状态" align="center" prop="upState" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gt:gtbook:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gt:gtbook:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改图书卡片对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="书名" prop="bookName">
          <el-input v-model="form.bookName" placeholder="请输入书名" />
        </el-form-item>
        <el-form-item label="出版时间" prop="bookTime">
          <el-input v-model="form.bookTime" placeholder="请输入出版时间" />
        </el-form-item>
        <el-form-item label="作者" prop="bookAuthor">
          <el-input v-model="form.bookAuthor" placeholder="请输入作者" />
        </el-form-item>
        <el-form-item label="豆瓣评分" prop="bookRank">
          <el-input v-model="form.bookRank" placeholder="请输入豆瓣评分" />
        </el-form-item>
        <el-form-item label="isbn" prop="bookIsbn">
          <el-input v-model="form.bookIsbn" placeholder="请输入isbn" />
        </el-form-item>
        <el-form-item label="封面" prop="bookImg">
          <el-input v-model="form.bookImg" placeholder="请输入封面" />
        </el-form-item>
        <el-form-item label="多图" prop="bookImgs">
          <el-input v-model="form.bookImgs" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="图书简介" prop="bookIntro">
          <el-input v-model="form.bookIntro" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="作者简介" prop="bookAuthorIntro">
          <el-input v-model="form.bookAuthorIntro" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="展现时间" prop="upTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.upTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择展现时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="up数量" prop="upNum">
          <el-input v-model="form.upNum" placeholder="请输入up数量" />
        </el-form-item>
        <el-form-item label="up状态" prop="upState">
          <el-input v-model="form.upState" placeholder="请输入up状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGtbook, getGtbook, delGtbook, addGtbook, updateGtbook } from "@/api/gt/book";

export default {
  name: "Book",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 图书卡片表格数据
      gtbookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookName: undefined,
        bookTime: undefined,
        bookAuthor: undefined,
        bookRank: undefined,
        bookIsbn: undefined,
        bookImg: undefined,
        bookImgs: undefined,
        bookIntro: undefined,
        bookAuthorIntro: undefined,
        upTime: undefined,
        upNum: undefined,
        upState: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询图书卡片列表 */
    getList() {
      this.loading = true;
      listGtbook(this.queryParams).then(response => {
        this.gtbookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bookId: undefined,
        bookName: undefined,
        bookTime: undefined,
        bookAuthor: undefined,
        bookRank: undefined,
        bookIsbn: undefined,
        bookImg: undefined,
        bookImgs: undefined,
        bookIntro: undefined,
        bookAuthorIntro: undefined,
        upTime: undefined,
        upNum: undefined,
        createTime: undefined,
        upState: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bookId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加图书卡片";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bookId = row.bookId || this.ids
      getGtbook(bookId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改图书卡片";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bookId != undefined) {
            updateGtbook(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addGtbook(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bookIds = row.bookId || this.ids;
      this.$confirm('是否确认删除图书卡片编号为"' + bookIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGtbook(bookIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gtbook/export', {
        ...this.queryParams
      }, `gt_gtbook.xlsx`)
    }
  }
};
</script>
